//
//  YLUserInfo.m
//  DoulinClient
//
//  Created by Apple on 17/3/25.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "YLUserInfo.h"
#import <objc/runtime.h>

@implementation YLUserInfo

/** 根据类动画获取类的所有属性,不要忘记导入#import <objc/runtime.h> */
- (NSArray *)perperiesWithClass:(Class)cls {
    NSMutableArray *perperies = [NSMutableArray array];
    unsigned int outCount;
    //动态获取属性
    objc_property_t *properties = class_copyPropertyList(cls, &outCount);
    //遍历person类的所有属性
    for (int i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        //NSLog(@"<-- %s, %d --> %@", __FUNCTION__, __LINE__, property_getAttributes(property));//查看 MJExtension
        const char *name = property_getName(property);
        NSString *s = [[NSString alloc] initWithUTF8String:name];
        [perperies addObject:s];
    }
    free(properties);
    return perperies;
}
/** 解档会触发 */
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self){
        NSArray * tmpAry = [self perperiesWithClass:[self class]];
        for (NSString *perperty in tmpAry){
            [self setValue:[aDecoder decodeObjectForKey:perperty] forKey:perperty];
        }
    }
    return self;
}
/** 归档会触发 */
- (void)encodeWithCoder:(NSCoder *)aCoder {
    NSArray * tmpAry = [self perperiesWithClass:[self class]];
    for (NSString * perperty in tmpAry) {
        [aCoder encodeObject:[self valueForKey:perperty] forKey:perperty];
    }
}

/*
- (id)initWithCoder:(NSCoder *)coder {
    if (self = [super init]) {
        self.uid = [coder decodeObjectForKey:@"uid"];
    }
    return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.uid forKey:@"uid"];
}
*/

@end
